x = int(input())
if len(str(x)) != 4:
    print(0)
else:
    st = []
    nums = [
        x,
        x % 1000,
        x // 10,
        x // 100,
        x % 100,
        x % 1000 // 10,
        x % 10000 // 1000,
        x % 1000 // 100,
        x % 100 // 10,
        x % 10
    ]
    if 1 in nums:
        nums.remove(1)
    for x in nums:
        koef = 0
        for count in range(2, x // 2 + 1):
            if x % count == 0:
                koef += 1
        if koef <= 0:
            st.append(x)
    if len(st) > 0:
        print(max(st))
    else:
        print(0)